package com.guaoran.newsdata.provider.controller;

import com.guaoran.newsdata.api.domain.News;
import com.guaoran.newsdata.provider.service.NewsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author gucheng
 * @Description 新闻模块
 * 2019-07-18 10:58
 */
@RestController
@RequestMapping("/news")
public class NewsController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private NewsService newsService;
    /**
     * 保存
     * @param news {@link News}
     * @Url http://127.0.0.1:8081/news/save
     * @return 如果成功，<code>true</code>
     */
    @PostMapping(value = "/save")
    public boolean saveObject(@RequestBody News news) {
        com.guaoran.newsdata.provider.entity.News newsSave =
                new com.guaoran.newsdata.provider.entity.News();
        BeanUtils.copyProperties(news,newsSave);
        newsService.save(newsSave);
        return true;
    }

    /**
     * 修改
     * @param news
     * @Url http://127.0.0.1:8081/news/update
     * @return
     */
    @PutMapping(value = "/update")
    public boolean updateNews(@RequestBody News news){
        com.guaoran.newsdata.provider.entity.News newsSave =
                new com.guaoran.newsdata.provider.entity.News();
        BeanUtils.copyProperties(news,newsSave);
        newsService.save(newsSave);
        return true;
    }

    /**
     * 删除
     * @param id
     * @Url http://127.0.0.1:8081/news/delete/11
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public boolean deleteNews(@PathVariable("id") Long id){
        newsService.delete(id);
        return true;
    }

    /**
     * 分页查询
     * @param pageable
     * @Url http://127.0.0.1:8081/news/page?size=2&page=0
     * @return
     */
    @GetMapping("/page")
    public Page<com.guaoran.newsdata.provider.entity.News> list(Pageable pageable){
        return newsService.findListByPage(pageable);
    }

    /**
     * 查询对象
     * @param id
     * @Url http://127.0.0.1:8081/news/get/1
     * @return
     */
    @GetMapping("/find/{id}")
    public com.guaoran.newsdata.provider.entity.News findNewsById(@PathVariable("id") Long id){
        return newsService.findNewsById(id);
    }


    /**
     * 查找所有的新闻
     * @Url http://127.0.0.1:8081/news/list
     * @return
     */
    @GetMapping(value = "/list")
    @HystrixCommand(fallbackMethod = "fallbackForFindAll",
        commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "150")
        }
    )
    public Collection<com.guaoran.newsdata.provider.entity.News> findAllNews() throws InterruptedException {
        // 如果随机时间 大于 150 ，那么触发容错
        Random random = new Random();
        int value = random.nextInt(200);
        Thread.sleep(value);
        logger.warn("findAllNews() costs " + value + " ms.");
        return newsService.findAllNews();
    }

    /**
     * {@link #findAllNews()} Fallback 方法
     * @return 返回空集合
     */
    public Collection<com.guaoran.newsdata.provider.entity.News> fallbackForFindAll() {
        logger.error("fallbackForFindAll() is invoked!");
        return Arrays.asList(new com.guaoran.newsdata.provider.entity.News());
    }
}
